package tinycloud.gui.dialog;

import tinycloud.gui.action.AddDirectoryPreferencesDialogAction;
import tinycloud.gui.action.CancelPreferencesDialogAction;
import tinycloud.gui.action.DeleteDirectoryPreferencesDialogAction;
import tinycloud.db.Option;
import tinycloud.db.Directory;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.*;
import tinycloud.gui.action.OkPreferencesDialogAction;

/**
 * Fenêtre des préferences.
 * @author Nicolas CHARREL
 */
public class PreferencesDialog extends JDialog {

    private JPanel mainPanel;
    private JPanel directoriesPanel;
    private JPanel buttons;
    private JButton addButton;
    private JButton deleteButton;
    private JPanel serverPanel;
    private JTextField serverTextField;
    private JTextField portTextField;
    private JPanel accountPanel;
    private JTextField usernameTextField;
    private JTextField passwordTextField;
    private JPanel buttonsPanel;
    private JButton okButton;
    private JButton cancelButton;
    
    public JList list;
    public DefaultListModel listModel;
    
    public PreferencesDialog() {
        setTitle("Préférences");
        setModal(true);
        
        Option option;

        mainPanel = new JPanel(new BorderLayout());

        
        listModel = new DefaultListModel();
        option = new Option();

        list = new JList(listModel);


        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setPreferredSize(new Dimension(300, 200));
        directoryPane.add(scrollPane);
        JPanel p = new JPanel();
        p.setLayout(new GridLayout(2, 1));

        

        // Crèation des onglets
        JTabbedPane tabbedPane = new JTabbedPane();

        // Onglet répertoires
        directoriesPanel = new JPanel(new FlowLayout());
        addButton = new JButton("Ajouter");
        addButton.addActionListener(new AddDirectoryPreferencesDialogAction(this));
        .add(addButton);
        deleteButton = new JButton("Supprimer");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new DeleteDirectoryPreferencesDialogAction(this));
        p.add(deleteButton);
        directoriesPanel.add(p);
        tabbedPane.addTab("Répertoires", directoriesPanel);

        // Onglet serveur
        serverPanel = new JPanel();
        serverPanel.setLayout(new GridLayout(2, 2));
        serverPanel.add(new JLabel("Serveur:"));
        serverTextField = new JTextField(option.getOption(""));
        serverPanel.add(serverTextField);
        serverPanel.add(new JLabel("port:"));
        portTextField = new JTextField(option.getOption(""));
        serverPanel.add(portTextField);
        tabbedPane.addTab("Serveur", serverPanel);

        // Onglet compte
        accountPanel = new JPanel();
        accountPanel.setLayout(new BoxLayout(accountPanel, BoxLayout.Y_AXIS));
        accountPanel.add(new JLabel("Nom d'utilisateur:"));
        usernameTextField = new JTextField(option.getOption(""));
        accountPanel.add(usernameTextField);
        accountPanel.add(new JLabel("Mot de passe:"));
        passwordTextField = new JTextField(option.getOption(""));
        accountPanel.add(passwordTextField);
        tabbedPane.addTab("Compte", accountPanel);
        
        mainPanel.add(tabbedPane, BorderLayout.CENTER);

        // Boutons OK et annuler
        buttonsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        okButton = new JButton("OK");
        okButton.addActionListener(new OkPreferencesDialogAction(this));
        buttonsPanel.add(okButton);
        cancelButton = new JButton("Annuler");
        cancelButton.addActionListener(new CancelPreferencesDialogAction(this));
        buttonsPanel.add(cancelButton);
        mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
        
        setContentPane(mainPanel);
        pack();
        setVisible(true);
    }

   public String getServer() {
        return serverTextField.getText();
   }

   public String getPort() {
        return portTextField.getText();
   }

   public String getUsername() {
        return usernameTextField.getText();
   }

   public String getPassword() {
       return passwordTextField.getText();
   }
    
}
